package alex.example.pruebas2;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class Stones extends Activity{
	
	private String[] lv_arr = {};
	private EditText filterText = null;
	ArrayAdapter<String> adapter = null;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stones); 
		//Filter
		filterText = (EditText) findViewById(R.id.editText1);
	    //Listener for the filter
		filterText.addTextChangedListener(filterTextWatcher);
		// Prepare an ArrayList of todo items
		ArrayList<String> listTODO = PrepareListFromXml();

		// Get a handle to the list view
		ListView lv = (ListView) findViewById(R.id.listView1);

		// Bind the data with the list
		lv_arr = (String[]) listTODO.toArray(new String[0]);
		adapter = new ArrayAdapter<String>(Stones.this,android.R.layout.simple_list_item_1, lv_arr);
		lv.setAdapter(adapter);

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	public ArrayList<String> PrepareListFromXml() {
		ArrayList<String> todoItems = new ArrayList<String>();
		XmlResourceParser todolistXml = getResources().getXml(R.xml.init);

		int eventType = -1;
		while (eventType != XmlResourceParser.END_DOCUMENT) {
			if (eventType == XmlResourceParser.START_TAG) {

				String strNode = todolistXml.getName();
				if (strNode.equals("piedra")) {
					todoItems.add(todolistXml.getAttributeValue(0));
					todoItems.add(todolistXml.getAttributeValue(2));
				}
			}

			try {
				eventType = todolistXml.next();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return todoItems;
	}

	private TextWatcher filterTextWatcher = new TextWatcher() {

	    public void afterTextChanged(Editable s) {
	    }

	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }

	    public void onTextChanged(CharSequence s, int start, int before,
	            int count) {
	        adapter.getFilter().filter(s);
	    }

		
	};

	

}
